package cn.eeepay.framework.dao.AgentProfitActivityUpdateRecord;


import cn.eeepay.framework.daoSuperbank.CreditcardApplyRecordDao;
import cn.eeepay.framework.model.AgentProfitActivityUpdateDetail;
import cn.eeepay.framework.model.AgentProfitActivityUpdateRecord;
import cn.eeepay.framework.model.AgentShareRule;
import cn.eeepay.framework.model.CreditcardApplyRecord;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

public interface AgentProfitActivityUpdateRecordDao {

    @Insert("insert into agent_profit_activity_update_record (agent_no, oper_entry, effect_time, create_person,parent_id)"
            + " values (#{info.agentNo}, #{info.operEntry},#{info.effectTime}, #{info.createPerson},#{info.parentId})")
    @SelectKey(statement = "select LAST_INSERT_ID()", keyProperty = "info.id", before = false, resultType = Long.class)
    Integer insertMainRecord(@Param("info")AgentProfitActivityUpdateRecord apau);

    @Insert("insert into agent_profit_activity_update_detail (record_id, detail,create_person)"
            + " values (#{recordId}, #{detail},#{createPerson})")
    Integer insertRecorDetail(@Param("detail")String text, @Param("recordId")Long id, @Param("createPerson")String username);

    @InsertProvider(type = AgentProfitActivityUpdateRecordDao.SqlProvider.class, method = "insertRecorDetailBatch")
    Integer insertRecorDetailBatch(@Param("list")List<AgentProfitActivityUpdateDetail> list);

    class SqlProvider{
        public String insertRecorDetailBatch(Map<String, Object> param){
            List<AgentProfitActivityUpdateDetail> list = (List<AgentProfitActivityUpdateDetail>) param.get("list");
            if(list == null || list.size() < 1){
                return "";
            }
            StringBuilder values = new StringBuilder();
            MessageFormat message = new MessageFormat("(#'{'list[{0}].recordId},#'{'list[{0}].detail}" +
                    ", #'{'list[{0}].createPerson}),");
            for(int i = 0; i < list.size(); i++){
                values.append(message.format(new Integer[]{i}));
            }
            final String valuesSql  = values.substring(1, values.length() - 2);//去掉最前面那个括号,和最后面的,)
            SQL sql = new SQL();
            sql.INSERT_INTO("agent_profit_activity_update_detail");
            sql.VALUES("record_id,detail,create_person" ,valuesSql);
            return sql.toString();
        }
    }
}
